package servlet;

import java.io.IOException;
import org.bson.Document;
import org.bson.types.ObjectId;

import com.mongodb.client.MongoCursor;

import mongo.Major;
import mongo.Department;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(description = "majorProcess", urlPatterns = { "/manager/major/majorServlet" })
public class majorServlet extends HttpServlet{
	private Major Major = new Major();
	private Department Department = new Department();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
	    response.setContentType("text/html;charset=UTF-8");
		
		String method = request.getParameter("method");
		System.out.println(method);
		if(method.equals("add")) {
			this.add(request, response);
		}else if(method.equals("prepareAdd")){
			this.prepareAdd(request, response);
		}else if(method.equals("delete")){
			this.delete(request, response);
		}else if(method.equals("update")){
			this.update(request, response);
		}else if(method.equals("prepareUpdate")){
			//this.prepareUpdate(request, response);
		}else if(method.equals("list")){
			this.list(request, response);
		}else{
//			request.getRequestDispatcher("404.jsp").forward(request, response); 添加错误页面
		}
	
	}
	
	
	public void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Boolean result = null ;
		String error = null;
		String loginUrl = null;
		try{
				String name = request.getParameter("name");
				String id = request.getParameter("id");
				String explain = request.getParameter("explain");
				String remark = request.getParameter("remark");
				String department = request.getParameter("department");
				ObjectId departmentid = new ObjectId(request.getParameter("departmentid"));
				Document newDeparment = new Document("name",name).
						append("explain", explain).
						append("remark", remark).
						append("department", department).
						append("departmentid", departmentid).append("id", id);
				if(Major.isExist(id)){
					//ID存在，不允许添加
					request.setAttribute("error","专业ID已存在！");
					request.getRequestDispatcher("majorServlet?method=prepareAdd").forward(request, response);
				}else{
					result = Major.add(newDeparment);
					if(result)
						this.list(request, response);
				}
			//TODO针对返回结果和所属模块，返回到相对应的页面
		}catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
            //TODO发生异常,跳转到对应页面
        }
		
	}
	
	public void prepareAdd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        try {
    		MongoCursor<Document> result = Department.list();
    		request.setAttribute("result", result);
    	    request.getRequestDispatcher("addMajor.jsp").forward(request, response);
    		//返回了所有系
    		//TODO针对返回结果和所属模块，返回到相对应的页面  
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          //TODO发生异常,跳转到对应页面
        }
	}
	
	public void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        try {
    		ObjectId _id = new ObjectId(request.getParameter("_id"));
    		Boolean result = Major.delete(_id);
    		if(result) {
				this.list(request, response);
			}
    		//TODO针对返回结果和所属模块，返回到相对应的页面  
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          //TODO发生异常,跳转到对应页面
        }
	}
	
//	public void prepareUpdate(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
//        try {
//        	MongoCursor<Document> result = Department.list();
//    		//返回了所有系
//    		//TODO针对返回结果和所属模块，返回到相对应的页面  
//        }catch(Exception e){
//            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
//          //TODO发生异常,跳转到对应页面
//        }
//	}
	
	public void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Boolean result = null;
		
        try {
    			ObjectId _id = new ObjectId(request.getParameter("_id"));
    			String id = request.getParameter("id");
    			String name = request.getParameter("name");
    			String explain = request.getParameter("explain");
    			String remark = request.getParameter("remark");
    			String department = request.getParameter("department");
    			ObjectId departmentid = new ObjectId(request.getParameter("departmentid"));
    			Document newDeparment = new Document("name",name).
    					append("explain", explain).
    					append("remark", remark).
    					append("department", department).
    					append("departmentid", departmentid).append("id", id);
    			result = Major.update(_id,newDeparment);
    			if(result) {
    				this.list(request, response);
    			}
    		//TODO针对返回结果和所属模块，返回到相对应的页面
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          //TODO发生异常,跳转到对应页面
        }
		
	}
	
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
        try {
    		MongoCursor<Document> result = Major.list();
    		request.setAttribute("result", result);
    	    request.getRequestDispatcher("majorList.jsp").forward(request, response);
    		//TODO针对所属模块，返回到相对应的页面   
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          //TODO发生异常,跳转到对应页面
        }

	}
	
}
